#import "SlideMenuView.h"

@interface SlideMenuView () {
    UINavigationController *centerNav;
    UIViewController *leftCtrl;
    
    CGFloat _leftMenuWidth;
    BOOL _isShowLeft;
    
    CGPoint _startPoint;
    CGFloat _startRootLeft;
    UITapGestureRecognizer *_tap;
    UIPanGestureRecognizer *_showGesture;
    UIPanGestureRecognizer *_moveGesture;
}
@end

@implementation SlideMenuView

- (id)initWithMainController:(UIViewController *)mainController withLeft:(UIViewController*)leftController {
    UINavigationController *centerNav = [[UINavigationController alloc] initWithRootViewController:mainController];
    self = [super init];
    _isShowLeft = NO;
    _leftMenuWidth = self.view.bounds.size.width/2.0;
    
    if (self) {
        [self setLeftCtrl:leftController];
        [self setCenterCtrl:centerNav];
        
        [self addChildViewController:centerNav];
        
        [self createGesture];
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
}

- (void)createGesture {
    UIViewController *vc = centerNav.viewControllers.firstObject;
    UIView *view = vc.view;
    
    _showGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(showGestureCallback:)];
    _showGesture.delegate = (id<UIGestureRecognizerDelegate>)self;
    [view addGestureRecognizer:_showGesture];
    
    _moveGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(moveGestureCallback:)];
    _moveGesture.delegate = (id<UIGestureRecognizerDelegate>)self;
    _moveGesture.enabled = NO;
    [view addGestureRecognizer:_moveGesture];
    
    _tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTap:)];
    _tap.enabled = NO;
    [view addGestureRecognizer:_tap];
}

- (void)setCenterCtrl:(UINavigationController *)rootViewController {
    if (centerNav) {
        [centerNav.view removeFromSuperview];
        centerNav = nil;
    }
    centerNav = rootViewController;
    if (centerNav) {
        UIView *view = centerNav.view;
        view.frame = self.view.frame;
        
        view.layer.shadowOpacity = 1;
        view.layer.shadowRadius = 10;
        view.layer.shadowColor = [UIColor darkGrayColor].CGColor;
        view.layer.shadowPath = [UIBezierPath bezierPathWithRect:self.view.bounds].CGPath;
        //view.layer.shouldRasterize = YES;
        //view.layer.rasterizationScale = [UIScreen mainScreen].scale;
        
        [self.view insertSubview:view atIndex:1];
    }
}

- (void)setLeftCtrl:(UIViewController *)leftViewController {
    leftCtrl = leftViewController;
    UIView *view = leftCtrl.view;
    view.frame = self.view.frame;
    [self.view insertSubview:view atIndex:0];
}

- (void)showMenu
{
    CGRect rect = self.view.frame;
    rect.origin.x = [UIScreen mainScreen].bounds.size.width - _leftMenuWidth;
    
    [UIView animateWithDuration:0.2 animations:^{
        centerNav.view.frame = rect;
        
    }completion:^(BOOL finished){
        _isShowLeft = YES;
        _showGesture.enabled = NO;
        _moveGesture.enabled = YES;
        _tap.enabled = YES;
        
        if (self.leftMenuBlock) {
            self.leftMenuBlock(YES);
        }
        
    }];
}

- (void)hideMenu:(BOOL)animated
{
    CGRect rect = self.view.frame;
    
    if (animated) {
        [UIView animateWithDuration:.2 animations:^{
            centerNav.view.frame = CGRectMake(0, 0, rect.size.width, rect.size.height);
        } completion:^(BOOL finished) {
            _isShowLeft = NO; 
            _showGesture.enabled = YES;
            _moveGesture.enabled = NO;
            _tap.enabled = NO;
            
            if (self.leftMenuBlock) {
                self.leftMenuBlock(NO);
            }
        }];
    } else {
        centerNav.view.frame = CGRectMake(0, 0, rect.size.width, rect.size.height);
        _isShowLeft = NO; 
        _tap.enabled = NO;
        _showGesture.enabled = YES;
        _moveGesture.enabled = NO;
        
        if (self.leftMenuBlock) {
            self.leftMenuBlock(NO);
        }
        
    }
}

- (void)toggleShowMenu
{
    if (!_isShowLeft) {
        [self showMenu];
    } else {
        [self hideMenu:YES];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

#pragma mark - Guesture
- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer {
    _startPoint = [gestureRecognizer locationInView:self.view];
    _startRootLeft = centerNav.view.frame.origin.x; 
    //printf("root left: %d\n", (int)_startRootLeft);
    NSLog(@"startPoint: %0.2f, %0.2f", _startPoint.x, _startPoint.y);
    return YES;
}


- (void)showGestureCallback:(UIPanGestureRecognizer*)pan {
    CGPoint vel = [pan velocityInView:self.view];
    if (vel.x > 700) {
        [self showMenu];
        return;
    }
    
    CGPoint locationPoint = [pan locationInView:self.view];
    CGFloat offsetX = locationPoint.x - _startPoint.x;
    
    if (pan.state == UIGestureRecognizerStateChanged) {
        if (locationPoint.x - _startPoint.x >= 6) {
            CGRect rootRect = centerNav.view.frame;
            printf("origin_x:%d, offsetX:%d\n", (int)rootRect.origin.x, (int)offsetX);
            rootRect.origin.x = offsetX;
            centerNav.view.frame = rootRect;
        }
    } else if (pan.state == UIGestureRecognizerStateEnded || pan.state == UIGestureRecognizerStateCancelled) {
        if (offsetX >= _leftMenuWidth/2) {
            [self showMenu];
        } else {
            [self hideMenu:YES];
        }
    }
}

- (void)moveGestureCallback:(UIPanGestureRecognizer*)pan {
    CGPoint vel = [pan velocityInView:self.view];
    if (vel.x < -700) {
        [self hideMenu:YES];
        return;
    }
    
    CGPoint locationPoint = [pan locationInView:self.view];
    CGFloat offsetX = locationPoint.x - _startPoint.x;
    CGFloat left = _startRootLeft + offsetX;
    
    if (pan.state == UIGestureRecognizerStateChanged) {
        CGRect rootRect = centerNav.view.frame;
        rootRect.origin.x = left;
        centerNav.view.frame = rootRect;
        
        
    } else if (pan.state == UIGestureRecognizerStateEnded || pan.state == UIGestureRecognizerStateCancelled) {
        //if (left < 50) {
            //[self hideMenu:YES];
        //}
        if (left < _leftMenuWidth/2) {
            [self hideMenu:YES];
            return;
        }
    }
    
}

- (void)handleTap:(UITapGestureRecognizer*)tap {
    if (_isShowLeft) {
        [self hideMenu:YES];
    }
}

@end


























































